package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	//1. 查询所有学生
	@Override
	public List<StudentVo> findAll() {
		//1.1 查询所有学生
		List<Student> students = studentMapper.selectAll();
		//1.2 转换为studentVo集合
		return getStudentVos(students);
	}

	//2. 分页查询
	@Override
	public PageResult<StudentVo> findByPage(Integer page, Integer pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page, pageSize);
		//2.2 开始查询
		//2.2.1 定义查询对象
		Example example = new Example(Student.class);
		List<Student> students = studentMapper.selectByExample(example);
		//2.2.2 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.2.3 将上面的对象集转换为StudentVo对象
		List<Student> result = studentPage.getResult();
		List<StudentVo> collect = getStudentVos(result);
		//2.3 返回
		return new PageResult<>(studentPage.getTotal(), collect);
	}

	//3. 条件查询带分页
	@Override
	public PageResult<StudentVo> search(Integer page, Integer pageSize, StudentVo vo) {
		//2.1 开始分页
		PageHelper.startPage(page, pageSize);
		//2.2 开始查询
		//2.2.1 定义查询对象
		Example example = new Example(Student.class);
		Example.Criteria criteria = example.createCriteria();
		if (vo != null) {
			if (StrUtil.isNotBlank(vo.getSname())) {
				criteria.andLike("sname", "%" + vo.getSname() + "%");
			}
			if (StrUtil.isNotBlank(vo.getAddr())) {
				criteria.andLike("addr", "%" + vo.getAddr() + "%");
			}
			if (vo.getCid() != null && vo.getCid() != 0) {
				criteria.andEqualTo("cid", vo.getCid());
			}
		}
		List<Student> students = studentMapper.selectByExample(example);
		//2.2.2 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.2.3 将上面的对象集转换为StudentVo对象
		List<Student> result = studentPage.getResult();
		List<StudentVo> collect = getStudentVos(result);
		//2.3 返回
		return new PageResult<>(studentPage.getTotal(), collect);
	}

	// 3. 添加学生
	@Override
	public void add(StudentVo vo) {
		//3.1 将vo转换为Student对象
		Student student = new Student();
		BeanUtil.copyProperties(vo, student, true);
		//3.2 添加学生
		studentMapper.insert(student);
	}
	//4. 修改学生
	@Override
	public void update(StudentVo vo) {
		//4.1 将vo转换为Student对象
		Student student = new Student();
		BeanUtil.copyProperties(vo, student, true);
		//4.2 修改学生
		studentMapper.updateByPrimaryKey(student);
	}
	// 5. 删除单个学生或批量删除
	@Override
	public void delete(Integer[] sids) {
		Example example = new Example(Student.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("sid", Arrays.asList(sids));
		studentMapper.deleteByExample(example);
	}

	// 将List<Student>转换为List<StudentVo>对象
	private List<StudentVo> getStudentVos(List<Student> result) {
		return result.stream().map(m -> {
			Classes classes = classesMapper.selectByPrimaryKey(m.getCid());
			StudentVo vo = new StudentVo();
			BeanUtil.copyProperties(m, vo, true);
			vo.setCname(classes.getCname());
			return vo;
		}).collect(Collectors.toList());
	}
}
